<div class="full-screen">
    <header class="bg-red">
    <div class="a-bar">
        <a href="javascript:history.back()" class="a-back">
            <i class="arr left"></i>
            返回
        </a>
        <div class="a-name" style="color: #fff">
            购物车
        </div>
        <div class="a-right">
            <a class="btn red " style="position: absolute;right: .2rem;color: #fff" id="clear_btn">清空购物车</a>
        </div>
        <div class="a-right">
            <a href="#" class="btn red J_settlement" style="display: none;">
                	结算
                <i class="arr right"></i>
            </a>
        </div>
    </div>
    </header>
    <div class="total<{if $aCart.items_quantity==0}> hide<{/if}>" id="J_total">
        <div class="">
            <div class="total-inner">
                <span class="total_type"><{$aCart.items_count}></span>种商品,共<span class="num"><{$aCart.items_quantity}></span>件,  <{if $check_login=="login"}>总价:
                 <span class="price"><{$aCart._cookie.CART_TOTAL_PRICE|cur_odr}></span>
                <!-- <span class="price"><{$aCart.subtotal_prefilter_after|cur_odr}></span> -->
                <{/if}>  	
                <a href="#" class="checkout J_settlement">结算</a>
            </div>
        </div>
    </div>
    <div class="full-padding">
        <div class="cart-pt">
            <{if $is_empty}>
            <{include file="wap/cart/cart_empty.html" app=b2c}>
            <{else}>
            <!-- 订货单主体 -->
            <{include file="wap/cart/cart_main.html" app=b2c}>
            <{/if}>
        </div>
    </div>
</div>
<script>
(function(){
    $(window).scroll(function(e){
        if($(this).scrollTop() > 95){
            $('#J_total').addClass('fixed');
        }else{
            $('#J_total').removeClass('fixed');
        }
    });

    //清空购物车
    $("#clear_btn").on("click",function(){
       
        if(confirm("确定清空购物车？")){
            $.post("<{link app=b2c ctl=wap_cart act=remove_cart}>",function(re){
                re=JSON.parse(re);
                if(re.success){
                    alert(re.success);
                    location.reload();
                }
            })
        }
        
    });
    //记录滚动条
    if(sessionStorage.scrollTop_cart!=0){
        document.body.scrollTop=sessionStorage.scrollTop_cart;
        document.documentElement.scrollTop=sessionStorage.scrollTop_cart;
    }

    $(document).on("scroll",function () {
        sessionStorage.scrollTop_cart=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
    });

    $('.J-pre').bind('click',function(e){
        $(this).toggleClass('act');
        $(this).parents('.pt-h-item').find('.pre-info').toggleClass('hide');
    });

    $('#order_promotion .d-line').bind('click',function(){
        $('#J_pre_info').toggleClass('hide');
        $(this).find('.pre-list').toggleClass('hide');
    });
    
    //add by hzk 2017-5-24  增加提示时间
    function popuphzk(msg) {
	  alert(msg );
    }
    //add end
  //== 赠品封顶值提示方法 add by hzk 2017-5-18
//== 赠品封顶值提示方法 add by hzk 2017-5-18
    	function checkmaxbuy(e1, value,inputtype) {
    		
    		 //add by hzk 2017-5-18
    	  var checkgift = e1.find('.action-quantity-input-havegift').val();	           
    	  if('havegift'==checkgift){
    		   var getmaxbuy = e1.find('.action-quantity-input-getmaxbuy').val();
    	      	
    					if(inputtype=='button' && getmaxbuy == value){
    	 		 //alert(2222222);   
    	 		popuphzk('该商品购买数量已达赠品优惠上限'+getmaxbuy+'，上限之后的购买数量将不赠送赠品，确定继续购买？');
    		            }else if(inputtype=='text' && getmaxbuy <= value){
    	 		 //alert(3333333);   
    	 		popuphzk('该商品仅在前'+getmaxbuy+'个商品赠送赠品，上限数量之后的购买数量将不赠送赠品，确定继续购买？');
    		            }
    	 	 //alert(getmaxbuy);   
    	  }
    	}
    

	// hzk
    // 设置商品数量
    function setQuantity(el,flag){
    	var getPeradd = parseInt(el.parent().find('.action-quantity-input-getPeradd').val());
        var ipt = el.parent().find('.num-ipt'),
            max = parseInt(el.parents('.pt-h-item').find('.max-num').val()),
            min = parseInt(el.parents('.pt-h-item').find('.min-num').val()),
            val = parseInt(ipt.val());
        
        var res =parseInt(0);
        peradd =  parseInt(("<{$add.isused}>" ==1) ?  "<{$add.pernum}>"  : parseInt(getPeradd)) ;
        
        min=peradd;
        var total=el.parents(".pt-h-item").find(".u-total");

		//add by peter 2017-05-05
		//购物车那 配件商品不可以增减数量为问题
		if(isNaN(min)){
			min = 0;

		}

		if(flag==2){
			checknum=val+peradd;
			checkmaxbuy(el.parent(), checknum,'button');
		}
		if(flag==3){
			checkmaxbuy(el.parent(), val,'button');
		}
	

         switch(flag){
            case 1:
                if(!/^\d+$/.test(val)) 
                    ipt.val(min);
                else if(val > min){
                    
                    //hzk
                    if(!(0 ==(val%peradd))){
                        res = val - (val%peradd);
                    }else{
                        res = val - peradd;
                    }
        /* alert(getPeradd); */
                    
                    ipt.val( res);
                }
                else if(val == min)
                    popup('此商品最少购买' + min + '件');
                break;
            case 2:
                if(val < max){
                    //hzk
                    if(!(0 ==(val%peradd))){
                         res = (val- parseInt(val%peradd)) +  parseInt(peradd);
                     }else{
                         res = parseInt(val) + parseInt(peradd);
                    }                   
                    ipt.val( +parseInt(res)  );
                } 
                 else if(val >= max)
            {
                //@mo edit
                ipt.val( max );
                popup('此商品最多只能购买' + max + '件');
            }
                break;
            case 3:
                if(!/^\d+$/.test(val)) {
                    ipt.val(min);
                } else if(val > max){
                    popup('此商品最多只能购买' + max + '件');
                    ipt.val(max);
                } else if(val < min){
                    popup('此商品最少购买' + min + '件');
                    ipt.val(min);
                }
                break;
        }
//        location.reload();
        function popup(msg) {
            var tpl = $('<div class="popup">' + msg + '</div>').appendTo(document.body);
            var timer = setTimeout(function(){
                tpl.remove();
            }, 3000);
        }

        var data = $('<form action=""></form>').append(el.parents('.pt-h-item').clone()).serialize();
        // alert(data);
        $.post('<{link app=b2c ctl=wap_cart act=update}>',data+"&response_type=true",function(re){
            var o = JSON.parse(re),
                promotion = $('#order_promotion .content'),
                unuse_rule = $('#unused_promotion .content'),
                ul = '';
            total.html(o.edit_ajax_data.mlv_price);
            if(o.promotion){
                if(o.promotion.order){
                    $.each(o.promotion.order,function(){
                        ul += '<li><b class="f-red">['+this.desc_tag+']</b>'+this.name+'</li>';
                        window.location.href=window.location.href;
                    });
                    promotion.html(ul);
                    ul = '';
                }else{
                    promotion.html('');
                }
            }else{
                promotion.html('');
                window.location.href=window.location.href;
            }
            if(o.unuse_rule){
                $.each(o.unuse_rule,function(){
                    ul += '<li><b class="f-red">['+this.desc_tag+']</b>'+this.name+'</li>';
                unuse_rule.html(ul);
                });
            }else{
                unuse_rule.html('');
            }
       
            el.parents('.pt-h-item').find('.u-total-child').html(o.edit_ajax_data.buy_price);
            $('#J_total .price').html(o.sub_total.promotion_subtotal);
            $('#J_total .num').html(o.number.cart_number);
        });

    }
    $('.J-pt-num').bind('click',function(e){
        var el = $(e.srcElement),
            n = 0;
        if( !el.hasClass('btn') )return;

        if( el.hasClass('minus') )n=1;
        if( el.hasClass('plus') )n=2;

        setQuantity(el,n);
//        location.reload();
    });
    $('.num-ipt').bind('change',function(){
        setQuantity( $(this),3 );
    });

    // 移除商品
    $('.J-remove').bind('click',function(e){
        if(!confirm('确认移除商品“'+$(this).parents('li').find('.pt-h-name').html().trim()+'”？'))return false;
        var oo = $(e.target);
        var data = $('<form action=""></form>').append($(this).parents('.pt-h-item').clone()).serialize();
        $.post($(this).attr('href'),data+"&response_type=true",function(re){
            var rs = JSON.parse(re);
            if(rs.is_empty) return location.reload();
            $('#J_total .price').html(rs.sub_total.promotion_subtotal);
            $('#J_total .num').html(rs.number.cart_number);
            $('#J_total .total_type').html(rs.number.cart_count);
            if(rs.error) return alert(rs.error);
            oo.parents('.pt-h-item').remove();


        });
        return false;
    });

    // 初始化订货单的收藏按钮状态
    var favmid = $.fn.cookie('S[MEMBER]');
    var gfav = $.fn.cookie("S[GFAV]["+favmid+"]");
    var gfav = decodeURIComponent(gfav);
    $('.favlist').each(function(index){
        if( gfav.split(',').indexOf( $(this).attr('data-gid') ) > -1 )
        {
            $(this).removeClass('btn-addfav').addClass('btn-faved').html('已收藏');
        }else{
            $(this).removeClass('btn-faved').addClass('btn-addfav').html('收藏');
        }
    });

    //收藏商品
    $('.btn-addfav').bind('click',function(){
        var $this = $(this);
        if($this.hasClass('btn-faved')) return;
        var data = 'gid='+$this.attr('data-gid')+'&type=goods';
        $.post('<{link app=b2c ctl=wap_member act=ajax_fav}>',data,function(re){
            //var re = $.parseJSON(re);
            //if(re.success){
                //$this.hide().next().show();
                $this.removeClass('btn-addfav').addClass('btn-faved').html('已收藏');
            //}
        });
        return false;
    });


    // 订货单结算
    $('.J_settlement').bind('click',function(e){
        <{if $limited_amount != null}>
        var order = $("#J_total .price").text();
        var limited_amount = "<{$limited_amount}>";
        var num= order.replace("￥","");
        if(parseInt(num)<limited_amount){
            alert('订单总额不能低于'+limited_amount);
             e.stop();
          }else{
            var $form=$("<form>");
           $form.attr({"action":"<{link app=b2c ctl=wap_cart act=checkout}>","method":"post"});
           console.log($form.clone());
            $("body").append($form);
            $form.submit();

//            $.post("<{link app=b2c ctl=wap_cart act=checkout}>",function (re) {
//                console.log(re);
//            })
            return false;
          }
          <{else}>
            var $form=$("<form>");
            $form.attr({"action":"<{link app=b2c ctl=wap_cart act=checkout}>","method":"post"});
            console.log($form.clone());
            $("body").append($form);
            $form.submit();
          <{/if}>
    });
})();
</script>


